<template>
  <div class="app-container">
    <Search @reset="reset" @search="handleSearch">
      <div>
        <label>评论内容</label>
        <el-input
          v-model="searchParams.content"
          placeholder="请输入评论内容"
          clearable
          @change="handleSearch"
        ></el-input>
      </div>
      <div>
        <label>评论人昵称</label>
        <el-input
          v-model="searchParams.user_nickname"
          placeholder="请输入评论人昵称"
          clearable
          @change="handleSearch"
        ></el-input>
      </div>
      <div>
        <label>文章表题</label>
        <el-input
          v-model="searchParams.article_title"
          placeholder="请输入文章表题"
          clearable
          @change="handleSearch"
        ></el-input>
      </div>
    </Search>
    <vxe-grid v-bind="gridConfig" class="sticky-header">
      <template #toolbar_buttons> </template>
      <template #article_title="{ row }"> 《{{ row.article.article_title }}》 </template>
      <template #operate="{ row }">
        <el-button link type="primary" @click="operateRef.open({ type: 'edit', comment_id: row.comment_id })"
          >修改</el-button
        >
        <!-- <el-button link @click="operateRef.open({ type: 'detail', article_id: row.article_id })">详情</el-button> -->
        <el-popconfirm width="280" title="对应子评论也会删除，确定删除吗?" @confirm="handleDelete(row.comment_id)">
          <template #reference>
            <el-button link type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
      <template #pager>
        <vxe-pager
          v-model:current-page="searchParams.pageNum"
          v-model:page-size="searchParams.pageSize"
          :total="searchParams.total"
          @page-change="getList"
        >
        </vxe-pager>
      </template>
    </vxe-grid>
  </div>

  <operate-component ref="operateRef" :options="options" @refresh-list="getList"></operate-component>
</template>

<script lang="ts" setup>
import Search from "@/components/Search/index.vue"
import { reactive, onMounted, ref } from "vue"
import { VxeGridProps } from "vxe-table"
import { ElMessage } from "element-plus"
import { getListByPage, articleCommentDelete } from "@/api/articleManage/comment"
import { getAllUser } from "@/api/userManage/index"
import { Options } from "./type"
import operateComponent from "./operate.vue"

defineOptions({
  name: "ArticleComment"
})

const operateRef = ref()
const searchParams = reactive(setDefaultSearchParams())
const options = reactive<Options>({
  tag: [],
  user: []
})
const gridConfig = reactive<VxeGridProps>({
  id: "ArticleComment", // 表格唯一标识
  customConfig: {
    storage: true
  },
  toolbarConfig: {
    custom: true,
    slots: {
      buttons: "toolbar_buttons"
    }
  },
  columns: [
    { type: "seq", width: 50 },
    { title: "评论id", field: "comment_id" },
    { title: "评论内容", field: "content" },
    { title: "评论人昵称", field: "user.nickname" },
    {
      title: "评论文章",
      field: "article.article_title",
      slots: {
        default: "article_title"
      },
      sortable: true,
      showOverflow: true
    },
    {
      title: "回复评论ID",
      field: "parentComment.comment_id"
    },
    {
      title: "回复评论人昵称",
      field: "parentComment.user.user_nickname"
    },
    { title: "日期", field: "createdAt" },
    {
      title: "操作",
      field: "operate",
      slots: { default: "operate" },
      showOverflow: false,
      width: 180
    }
  ],
  data: []
})

function handleDelete(comment_id: number) {
  articleCommentDelete({ comment_id }).then(() => {
    ElMessage.success("删除成功")
    getList()
  })
}

function handleSearch() {
  searchParams.pageNum = 1
  getList()
}

function reset() {
  Object.assign(searchParams, setDefaultSearchParams())
  handleSearch()
}

function setDefaultSearchParams() {
  return {
    content: "",
    article_title: "",
    user_nickname: "",
    pageNum: 1,
    pageSize: 10,
    total: 0
  }
}

function getList() {
  getListByPage(searchParams).then((res: any) => {
    gridConfig.data = res.data.list
    searchParams.total = res.data.total
  })
}

onMounted(() => {
  getList()
  getAllUser().then((res) => {
    options.user = res.data
  })
})
</script>

<style lang="scss" scoped></style>
